const app = getApp()
var utils = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
var MD5Util = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: {

      image: '/images/goodsDetail/21.png',
      introduce: '高跟鞋伴侣二号鞋垫',
      nowP: 29, //现价
      oldP: 29.5,
      freight: 10, //快递
      Sales: 21, //销量
      detail: '高跟鞋伴侣二号鞋垫高跟鞋伴侣二号鞋垫高跟鞋伴侣二号鞋垫高跟鞋伴侣二号鞋垫高跟鞋伴侣二号鞋垫'
    },

    person: {
      name: '章鱼哥',
      phone: '13522222225',
      address: '四川省成都市青羊区青龙街青华巷青华小区2栋4单元3楼303'
    },
    chose: true, //点击兑换切换
    showmask: false, //点击出现弹窗
    jian: false,
    jia: true,
    num: 1, //需要购买产品个数
    exchange: true, //点击兑换
    point: 0, //消费总价
    canclick: true,
    clickColor:0,
    clickSize:0,
    color: '紫色',
    size: '37',
    // colorChose: ['红色', '绿色', '白色', '粉色'],
    colorChose:[ //state为0为缺货
      { id: 0, state: 0, name: '红色' },
      { id: 1, state: 1, name: '绿色' },
      { id: 2, state: 2, name: '白色' },
      { id: 3, state: 3, name: '粉色' }
    ],
    sizeChose: [ //state为0为缺货
      { id: 0, state: 0, name: '37' },
      { id: 1, state: 1, name: '38' },
      { id: 2, state: 2, name: '39' },
      { id: 3, state: 3, name: '40' }
    ]

  },
  //点击出弹窗
  exchange() {
    this.setData({
      showmask: true
    })
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear',
      delay: 0
    });
    animation.opacity(1).bottom(0).step();
    this.setData({
      ani: animation.export()
    })
  },
  //关闭弹窗
  closeMask() {
    this.setData({
      showmask: false,
      chose: true
    })
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear',
      delay: 0
    });
    animation.opacity(0).bottom('-1000rpx').step();
    this.setData({
      ani: animation.export()
    })
  },
  closeMaskRn() {
    this.closeMask()
    this.setData({
      chose: true
    })
  },
  //减
  reduce() {
    let i = this.data.num;
    i--;
    if (i <= 1) {
      i = 1;
      this.setData({
        jian: false
      })
    }
    this.setData({
      num: i,
      point: i * this.data.product.nowP + this.data.product.freight
    })
  },
  //加
  add() {
    let i = this.data.num;
    i++;
    this.setData({
      jian: true,
      num: i,
      point: i * this.data.product.nowP + this.data.product.freight
    })
  },
  //点击购买
  exchangeFn() {
    var that = this;
    if (that.data.person) {
      wx.showLoading({
        title: '正在下单...',
      })
      
      var sucFunc = function (that, res) {
        if (res.data != 400) {

          var suc = function (that, res) {
            wx.hideLoading();
            var str = String(res.data.timestamp)
            var sign = '';
            var signA = "appId=" + res.data.appid + "&nonceStr=" + res.data.noncestr + "&package=prepay_id=" + res.data.prepayid + "&signType=MD5&timeStamp=" + res.data.timestamp;
            var signB = signA + "&key=WELLNESweiershuzubuguanliguanjia";
            sign = MD5Util.MD5(signB).toUpperCase();
            wx.requestPayment({
              'timeStamp': str,
              'nonceStr': res.data.noncestr,
              'package': "prepay_id=" + res.data.prepayid,
              'signType': 'MD5',
              'paySign': sign,
              'success': function (res) {
                that.setData({
                  chose: false
                })
              },
              'fail': function (res) {
                console.log(res);
                wx.showModal({
                  title: '提示',
                  content: '支付失败，请稍后重试！',
                  success: function (res) {
                    return
                  }
                })
              }
            })
          }
          var res = { openid: app.d.openid, fee: that.data.product.price * that.data.num, order_on: res.data }
          utils.wxRequest(that, app.d.path, "Request/index", res, suc);
        } else {
          wx.showModal({
            title: '提示',
            content: '下单失败,请稍后重试',
            success(res) {
              return false
            }
          })
        }

        wx.hideLoading();
      }
      var data = {
        id: that.data.product.id,
        openid: app.d.openid,
        price: that.data.product.price * that.data.num,
        color: that.data.color,
        size:that.data.size,
        sum: that.data.num,
        address: that.data.person.sheng + that.data.person.city + that.data.person.quyu + that.data.person.address,
        phone: that.data.person.phone,
        ursername: that.data.person.name
      }
      utils.wxRequest(that, app.d.path, "Goods/order", data, sucFunc);
    } else {
      wx.showToast({
        title: '请先添加收获地址',
        icon: 'none',
        duration: 1000
      })
    }
  },
  //选择颜色
  clickColorFn(e){
    // console.log(e.currentTarget.dataset.color)
    let state = e.currentTarget.dataset.state
    if(state == 0){ //缺货
      return false
    }else{
      this.setData({
        clickColor: e.currentTarget.dataset.color,
        color: e.currentTarget.dataset.name  //选择的颜色
      })
    }
  },
  //选择尺码
  sizeChoseFn(e) {
    // console.log(e.currentTarget.dataset.color)
    let state = e.currentTarget.dataset.state
    if (state == 0) { //缺货
      return false
    } else {
      this.setData({
        clickSize: e.currentTarget.dataset.color,
        size: e.currentTarget.dataset.name  //选择的尺码
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      point: this.data.product.nowP + this.data.product.freight
    })
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    that.setData({
      imgPath: app.d.imgPath
    })
    var sucFunc = function (that, res) {
      that.setData({
        product: res.data.res,
        swiperimg: res.data.images,
        colorChose:res.data.norm,
        color:res.data.norm[0].color,
        size:res.data.norm[0].size[0],
        person:res.data.address
      })
      WxParse.wxParse('article', 'html', res.data.res.content, that, 5)
      wx.hideLoading();
    }
    var data = { id: options.id,openid:app.d.openid }
    utils.wxRequest(that, app.d.path, "Goods/detail", data, sucFunc);

  },
  address: function () {
    wx.navigateTo({
      url: '/pages/address/address'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})